package com.wwy.arithmetic.suanfa;

import com.wwy.arithmetic.suanfa.entry.TreeNode;
import sun.reflect.generics.tree.Tree;

import java.util.Random;

/**
 * 将数组转换成平衡二叉树
 */
public class PingHengTree {

    static  Random random = new Random();

    public static void main(String[] args) {
        //测试数据
        int[] nums = {1, 3, 5, 7, 9};
        TreeNode treeNode = getTreeNode(nums, 0, nums.length);
        System.out.println("treeNode = " + treeNode);
    }

    /**
     * 每次都定位到下一次的中间
     * @param nums
     * @param left
     * @param right
     * @return
     */
  static   TreeNode getTreeNode(int[] nums, int left, int right) {
        if(left>right){
            return null;
        }
        int centNum = (left+right)/2;
        if(centNum>=nums.length){
            return null;
        }
        TreeNode node = new TreeNode(nums[centNum]);
        node.setLeft(getTreeNode(nums,left,centNum-1));
        node.setRight(getTreeNode(nums,centNum+1,right));
        return node;
    }
}
